' ene exporter
' generates the "compacted version", with variable # of enemies + index

Sub usage
	Print "Usage:"
	Print 
	print "$ eneexp3c enems.ene out.h prefix [yadjust] [nohotspots] [gencounter]"
	Print
End Sub

Function inCommand (a As String) As Integer
	Dim As Integer i
	i = 1
	While (Command (i) <> "")
		If Command (i) = a Then Return -1
		i = i + 1
	Wend
	Return 0
End Function

Dim As Integer fIn, fOut, ctrr
Dim As uByte d, a, b
Dim As String Dummy, prefix
Dim As Integer mapW, mapH, nEnems, mapPants, nPant, i, j, hl, yadjust, noHotspots
Dim As uByte t, xy1, xy2, mn
Dim As Integer typeCounters (255)
Dim As Integer enemTypeCounters (255)
Dim As uByte miniBin (255)
Dim As Integer miniBinIdx
Dim As Integer screenOn (255)
Dim As Integer totalBytes
Dim As Integer genCounter, genAllCounters

Print "MK1 v0.1R eneexp3c v0.2 20190715 ~ ";

'' Parse the command line

If Command (3) = "" Then usage: End
prefix = Command (3)

yadjust = Val (Command (4))
Print "yadjust: " & yadjust & " ~ ";
If inCommand ("noHotspots") Then 
	noHotspots = -1 : Print "No hotspots ~ "; 
Else 
	noHotspots = 0
End If
genCounter = inCommand ("gencounter")
genAllCounters = inCommand ("genallcounters")


' Outputs 4 bytes per entry
' T for type
' XY for ini
' XY for end
' MN for properties
' T = 0xff means end marker for current screen!

' Then generates an index.

' .ENE contains...
' t x1 y1 x2 y2 n s1 s2
' n is speed
' s1, s2 are unused.

fIn = FreeFile
Open Command (1) For Binary As #fIn
fOut = FreeFile
Open Command (2) For Output As #fOut

' Header
dummy = Input (256, fIn)
Get #fIn, , d: mapW = d
Get #fIn, , d: mapH = d
Get #fIn, , d: Get #fIn, , d
Get #fIn, , d: nEnems = d

mapPants = mapW * mapH

Print #fOut, "// " & Command (2)
Print #fOut, "// Generated by eneexp3c.exe v0.2 [20190715] (MK1 version)"
Print #fOut, "// Copyleft 2016, 2019 The Mojon Twins"
Print #fOut, ""
Print #fOut, "// Enems structure is {T YX1 YX2 P} x " & nEnems & " x MAP_W x MAP_H"
Print #fOut, "// y coordinates are adjusted by " & yadjust & " positions"
Print #fOut, ""

totalBytes = 0

' Read pants, store current, write back.
For i = 1 To mapPants

	miniBinIdx = 0
	ctrr = 0
	For j = 1 to nEnems
		Get #fIn, , t
		Get #fIn, , a: Get #fIn, , b: If t <> 0 Then b = b + yadjust
		xy1 = (b Shl 4) Or (a And 15)
		Get #fIn, , a: Get #fIn, , b: If t <> 0 Then b = b + yadjust
		xy2 = (b Shl 4) Or (a And 15)
		Get #fIn, , mn
		Get #fIn, , d: Get #fIn, , d

		' t == 0 means no enemy defined
		If t Then
			enemTypeCounters (t) = enemTypeCounters (t) + 1
			miniBin (miniBinIdx) = t: miniBinIdx = miniBinIdx + 1
			miniBin (miniBinIdx) = xy1: miniBinIdx = miniBinIdx + 1
			miniBin (miniBinIdx) = xy2: miniBinIdx = miniBinIdx + 1
			miniBin (miniBinIdx) = mn: miniBinIdx = miniBinIdx + 1
			ctrr = ctrr + 1
		End If
	Next j

	screenOn (i-1) = ctrr
	If ctrr Then
		Print #fOut, "const unsigned char enems_" & prefix & "_" & Lcase (Hex (i - 1, 2)) & " [] = {"
		Print #fOut, "	";
		miniBinIdx = 0
		For j = 1 To ctrr
			Print #fOut, "0x" & Lcase (Hex (miniBin (miniBinIdx), 2)) & ", ";: miniBinIdx = miniBinIdx + 1
			Print #fOut, "0x" & Lcase (Hex (miniBin (miniBinIdx), 2)) & ", ";: miniBinIdx = miniBinIdx + 1
			Print #fOut, "0x" & Lcase (Hex (miniBin (miniBinIdx), 2)) & ", ";: miniBinIdx = miniBinIdx + 1
			Print #fOut, "0x" & Lcase (Hex (miniBin (miniBinIdx), 2));: miniBinIdx = miniBinIdx + 1
			If j < ctrr Then Print #fOut, ", ";
			totalBytes = totalBytes + 4
		Next j
		If ctrr < 3 Then Print #fOut, ", 0": totalBytes = totalBytes + 1
		Print #fOut, "};"
		Print #fOut, ""
	End If
Next i

' Index
Print #fOut, "const unsigned char * const enems_" & prefix & " [] = {"

ctrr = 0
For i = 0 To mapPants - 1
	If ctrr = 0 Then Print #fOut, "	";
	If screenOn (i) Then 
		Print #fOut, "enems_" & prefix & "_" & Lcase (Hex (i, 2));
	Else
		Print #fOut, "0";
	End If
	If i < mapPants - 1 Then Print #fOut, ", ";
	ctrr = ctrr + 1: If ctrr = 8 Then ctrr = 0: Print #fOut, ""
	totalBytes = totalBytes + 2
Next i
If ctrr Then Print #fOut, ""
Print #fOut, "};"
Print #fOut, ""
Print #fOut, "// Bytes: " & totalBytes & " vs. MAP_WxMAP_Hx12 = " & mapPants * 12 & " (" & Int(totalBytes*100/(mapPants*12)) & "%)"
Print #fOut, ""

Print "Enems: " & totalBytes & " bytes ~ ";

'For i = 1 To 255
''	If enemTypeCounters (i) Then Print #fOut, "#define MAX_ENEMS_" & Ucase (prefix) & "_TYPE_" & Ucase (Hex (i, 2)) & "	" & enemTypeCounters (i)
'next i
If genCounter Then
	print #fOut, "#define KILLABLE_ENEMS_" & prefix & " " & (enemTypeCounters (1) + enemTypeCounters (2) + enemTypeCounters (3) + enemTypeCounters (5)+ enemTypeCounters (6) + enemTypeCounters (7) + enemTypeCounters (8) + enemTypeCounters (9))
	Print #fOut, ""
End If

Print #fOut, ""

If Not noHotspots Then
	Print #fOut, "// Hotspots structure is {T YX} x MAP_W x MAP_H"
	Print #fOut, "const unsigned char hotspots_" & prefix & " [] = {"
	hl = 0
	For i = 1 To mapPants
		Get #fIn, , xy1
		Get #fIn, , xy2: xy2 = xy2 + yadjust
		xy1 = (xy2 Shl 4) Or (xy1 And 15)
		Get #fIn, , t: If t = 0 Then xy1 = 0: xy2 = 0
		if hl = 0 Then Print #fOut, "	";
		Print #fOut, "0x" & Hex (t, 2) & ", 0x" & Hex (xy1, 2);
		If i < mapPants Then Print #fOut, ", ";
		hl = (hl + 1) And 3
		If hl = 0 Then Print #fOut, ""
		typeCounters (t) = typeCounters (t) + 1
	Next i
	Print #fOut, "};"
	Print #fOut, ""
	
	For i = 0 To 255
		If typeCounters (i) <> 0 then
			' Print #fOut, "// Wrote " & typecounters (i) & " hotspots of type " & i
			Print #fOut, "#define MAX_HOTSPOTS_" & Ucase (prefix) & "_TYPE_" & i & "	" & typeCounters (i)	
		End If
	Next i

	Print "Hotspots: " & (mapPants * 2) & " bytes ~ ";
End If

Close

Print "DONE!"
